home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3787 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: <75151.03563@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Destructors in Functions
  5. Date: 26 Jan 1996 02:32:20 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4e9efk$t7e@dub-news-svc-6.compuserve.com>
  8. NNTP-Posting-Host: dd35-159.compuserve.com
  9. Content-Type: text/plain
  10. Content-length: 2204
  11. X-Newsreader: AIR Mosaic (16-bit) version 3.10.08.25
  12.  
  13.  
  14. >> If you pass an object to a function, the language makes a bitwise copy
  15. >> of it, local to the function (Assuming no copy contructor is defined).
  16. >> Then, on the way out, the objects destructor is called.
  17. >>
  18. >> This will cause problems if the object has any pointers in it (if they
  19. >> are relased by the destructor, as they 'should' be).
  20. >>
  21. >> The question is, why call a destructor on an object for which no
  22. >> contstructor was called ? The compiler has probably put the local
  23. >> copy on the stack, and could just pop it off on the way out.
  24. >>
  25. >> Calling a destructor, automatically, for which no constructor was called
  26. >> seems to be a non-useful feature.  Does anyone know why this was
  27. >> included in the language definition ?
  28. >>
  29.  
  30. First of all, in the example you give, a constructor is called for the object - 
  31. it is just the default copy ctor which is generated by the compiler.
  32.  
  33. When an object is passed, by value, into a function the compiler makes a
  34. temporary copy of the object, using the object's copy constructor.  If no
  35. copy ctor was explicitly defined, the compiler will generate one - which will
  36. be a member-wise copy of the object.  
  37.  
  38. When function is exited, the temporary object goes out of scope, so it's 
  39. destructor is called. 
  40.  
  41. If a class allocates memory, it should follow the "Law of the Big Three".
  42. Which basically states if you need to write any of a : copy contructor, an
  43. assignment operator, or a destructor for a class - you really need all three.
  44.  
  45. If your class allocates memory, it should have a destructor to free this
  46. memory.  It also needs a copy ctor and an assignment operator, so that 
  47. objects of the class can be copied correctly.
  48.  
  49. In your example function, if the object being passed in conformed to the
  50. Law of the Big Three, when it is passed in, its copy ctor would be called
  51. which would allocated and initialize the memory it needed.  When the
  52. function returned, its destructor would be called, which would free the
  53. memory.  Writing a class that allocates memory and does not have a
  54. dtor, a copy ctor, and an assignment operator, is a surefire recipe for
  55. disaster.
  56.  
  57. Hope this helps,
  58.  
  59. Tom Keane
  60. 75151, 03563
  61.  
  62.  
  63.  
  64.  
  65.  
  66.